<?php
// +—————————————————————————————————————————————————————————————————————
// | Created by Yunbao
// +—————————————————————————————————————————————————————————————————————
// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
// +—————————————————————————————————————————————————————————————————————
// | Author: https://gitee.com/yunbaokeji
// +—————————————————————————————————————————————————————————————————————
// | Date: 2022-04-30
// +—————————————————————————————————————————————————————————————————————

class Model_User extends PhalApi_Model_NotORM {
	/* 用户全部信息 */
	public function getBaseInfo($uid) {

		
		$info=DI()->notorm->user
				->select("id,user_nicename,avatar,avatar_thumb,sex,signature,province,city,area,birthday,age,mobile,bg_img")
				->where('id=?  and user_type="2"',$uid)
				->fetchOne();

		if($info){

			if($info['age']==-1){
				$info['age']="年龄未填写";
			}else{
				$info['age'].="岁";
			}

			if($info['city']==""){
				$info['city']="城市未填写";
				$info['hometown']="";
			}else{
				$info['hometown']=$info['province'].$info['city'].$info['area'];
			}	

			$info['avatar']=get_upload_path($info['avatar']);
			$info['avatar_thumb']=get_upload_path($info['avatar_thumb']);						
			$info['follows']=getFollows($uid);
			$info['fans']=getFans($uid);
			$info['praise']=getPraises($uid);
			$info['workVideos']=getWorks($uid);
			$info['likeVideos']=getLikes($uid);
			$info['bg_img']=get_upload_path($info['bg_img']);

			$info['signature']=ReplaceSensitiveWords($info['signature']); //个性签名过滤敏感词
			$info['user_nicename']=ReplaceSensitiveWords($info['user_nicename']); //昵称过滤敏感词
		}

		
		
					
		return $info;
	}
			
	/* 判断昵称是否重复 */
	public function checkName($uid,$name){
		$isexist=DI()->notorm->user
					->select('id')
					->where('id!=? and user_nicename=?',$uid,$name)
					->fetchOne();
		if($isexist){
			return 0;
		}else{
			return 1;
		}
	}
	/* 判断手机号码是否重复 */
	public function checkMobile($uid,$mobile){
		$isexist=DI()->notorm->user
					->select('id')
					->where('id!=? and mobile=?',$uid,$mobile)
					->fetchOne();
		if($isexist){
			return 0;
		}else{
			return 1;
		}
	}
	/* 修改信息 */
	public function userUpdate($uid,$fields){
		/* 清除缓存 */
		delCache("userinfo_".$uid);
		
		return DI()->notorm->user
					->where('id=?',$uid)
					->update($fields);
	}

	
	/* 关注 */
	public function setAttent($uid,$touid){

		//判断关注列表情况
		
		$isexist=DI()->notorm->user_attention
					->select("*")
					->where('uid=? and touid=?',$uid,$touid)
					->fetchOne();
		if($isexist){
			DI()->notorm->user_attention
				->where('uid=? and touid=?',$uid,$touid)
				->delete();
			return 0;
		}else{
			DI()->notorm->user_attention
				->insert(array("uid"=>$uid,"touid"=>$touid,"addtime"=>time()));




			
			return 1;
		}			 
	}	

	
	/* 关注列表 */
	public function getFollowsList($uid,$touid,$p,$key){
		$pnum=50;
		$start=($p-1)*$pnum;



		if($key!=0 &&!$key){
			$touids=DI()->notorm->user_attention
					->select("touid")
					->where('uid=?',$touid)
					->order("addtime desc")
					->limit($start,$pnum)
					->fetchAll();

		}else{

			$where="a.uid='{$touid}' and u.user_nicename like '%".$key."%'";
		
			$prefix= DI()->config->get('dbs.tables.__default__.prefix');

			$touids=DI()->notorm->user_attention->queryAll("select a.touid,u.user_nicename from {$prefix}user_attention as a left join {$prefix}user as u on a.touid=u.id where ".$where." order by addtime desc limit {$start},{$pnum}");
		}


		//敏感词树        
        $tree=trieTreeBasic();

		foreach($touids as $k=>$v){
			$userinfo=getUserInfo($v['touid'],$tree);
			if($userinfo){
				if($uid==$touid){
					$isattent=1;
				}else{
					$isattent=isAttention($uid,$v['touid']);
				}
				$userinfo['isattention']=$isattent;
				$touids[$k]=$userinfo;
			}else{
				DI()->notorm->user_attention->where('uid=? or touid=?',$v['touid'],$v['touid'])->delete();
				unset($touids[$k]);
			}
		}		
		$touids=array_values($touids);
		return $touids;
	}
	
	/* 粉丝列表 */
	public function getFansList($uid,$touid,$p){

		$pnum=50;
		$start=($p-1)*$pnum;
		$touids=DI()->notorm->user_attention
					->select("uid,addtime")
					->where('touid=?',$touid)
					->limit($start,$pnum)
					->fetchAll();

		//敏感词树        
        $tree=trieTreeBasic();
		
		foreach($touids as $k=>$v){
			$userinfo=getUserInfo($v['uid'],$tree);
			if($userinfo){
				$userinfo['isattention']=isAttention($uid,$v['uid']);
				$touids[$k]=$userinfo;
				$touids[$k]['attentiontime']=datetime($v['addtime']);
			}else{
				DI()->notorm->user_attention->where('uid=? or touid=?',$v['uid'],$v['uid'])->delete();
				unset($touids[$k]);
			}
			
		}		
		$touids=array_values($touids);
		return $touids;
	}	


	
		/* 个人主页 */
	public function getUserHome($uid,$touid){

		$info=getUserInfo($touid);				

		$info['follows']=NumberFormat(getFollows($touid));
		$info['fans']=NumberFormat(getFans($touid));
		$info['isattention']=(string)isAttention($uid,$touid);

		


		return $info;
	}
	

	/*获取用户喜欢的视频列表*/
	public function getLikeVideos($uid,$touid,$p){


		$pnum=18; //数字必须为18

		$start=($p-1)*$pnum;



		//获取用户喜欢的视频列表
		$list=DI()->notorm->user_video_like->where("uid=? and status=1 ",$touid)->order("addtime desc")->limit($start,$pnum)->fetchAll();

		if(!$list){
			return 1001;
		}

		//敏感词树        
        $tree=trieTreeBasic();

		foreach ($list as $k => $v) {
			
			$videoinfo=DI()->notorm->user_video->where("id=? and status=1 and isdel=0 ",$v['videoid'])->fetchOne();

			if(!$videoinfo){
				//DI()->notorm->user_video_like->where("videoid=?",$v['videoid'])->delete();
				unset($list[$k]);
				continue;
			}
            $video=handleVideo($uid,$videoinfo,$tree);

			$video['addtime']=date('Y-m-d H:i:s', $v['addtime']);
			$video['datetime']=datetime($v['addtime']);

			$video['isdel']='0';  //暂时跟getAttentionVideo统一(包含下面的)
			$video['isdialect']='0';

			$lista[]=$video;  //因为unset掉某个数组后，k值连不起来了，所以重新赋值给新数组

		}

		if(empty($lista)){
			$lista=array();
		}

		return $lista;
	}



	/* 充值规则 */
	public function getChargeRules(){


		$rules= DI()->notorm->charge_rules
			->select('id,coin,coin_ios,money,product_id,give')
			->order('orderno asc')
			->fetchAll();


		return 	$rules;
	}

	/* 我的钻石 */
	public function getBalance($uid){
		return DI()->notorm->user
				->select("coin")
				->where('id=?',$uid)
				->fetchOne();
	}


	//获取用户是否可开播、是否可上传视频
	public function checkLiveVipStatus($uid){

		$configpri=getConfigPri();

		$auth_islimit=$configpri['auth_islimit'];

		$result['video_status']='1';


		//判断后台是否开启发布视频需要身份认证状态
		$isauth=isAuth($uid);
		if($auth_islimit && !$isauth){ //认证开启 且用户未认证
			return 1001;
		}


		return $result;
	}

	
	









	//更换背景图
	public function updateBgImg($uid,$img){
		$result=DI()->notorm->user
			->where(['id'=>$uid])
			->update(['bg_img'=>$img]);

		if(!$result){
			return 1001;
		}

		return 1;
	}
	
}
